package com.example.recyclerdemo;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.app.Activity;
import android.app.SearchableInfo;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.recyclerdemo.databinding.ActivityMainBinding;
import com.example.recyclerdemo.databinding.ToolbarLayoutBinding;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private ToolbarLayoutBinding toolbarBinding;
    private List<Student> students;
    private StudentAdapter adapter;
    private Student currentStudent;
    private int currentIndex;
    private int currentPos;
    private StudentDAO studentDAO;

    private ActivityResultLauncher<Intent> launcher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
                @Override
                public void onActivityResult(ActivityResult result) {
                    Toast.makeText(MainActivity.this,"返回的结果",Toast.LENGTH_SHORT).show();
                    if (result != null && result.getResultCode() == RESULT_OK &result.getData() != null){
                        changeData();
                    }
                }
            }
    );


    public MainActivity() {
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        //<mearge>的binding方式
        toolbarBinding = ToolbarLayoutBinding.bind(binding.getRoot());
        setSupportActionBar(toolbarBinding.toolbar);

        //初始化数据
        initData();
        //初始化视图
        binding.recyclerview.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerview.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        adapter = new StudentAdapter(students);
        adapter.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View view) {
                StudentAdapter.ViewHolder viewHolder = (StudentAdapter.ViewHolder) view.getTag();
                currentPos = viewHolder.getAdapterPosition();
                adapter.setCurrentIndex(currentPos);

                final Intent intent = new Intent(MainActivity.this, StudentUpdateActivity.class);
                final Bundle bundle = new Bundle();
                bundle.putSerializable("student", students.get(currentPos));
                intent.putExtras(bundle);
                launcher.launch(intent);
            }
        });
        binding.recyclerview.setAdapter(adapter);
    }

    private void initData() {
        students = new ArrayList<>();
        studentDAO = new StudentDAO(this);
        students = studentDAO.getAllStudent();
    }

    private void changeData() {
        students.clear();
        students.addAll(studentDAO.getAllStudent());
        adapter.notifyDataSetChanged();
    }

    //根据关键字查询返回
    public List<Student> updateStudents(String kw){
        return studentDAO.getStudents(kw);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        final int itemId = item.getItemId();
        if (itemId == R.id.item_add) {
            final Intent intent = new Intent(MainActivity.this, StudentUpdateActivity.class);
            launcher.launch(intent);
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_student, menu);
        SearchView searchView = (SearchView) menu.findItem(R.id.item_search).getActionView();
        initSearchView(searchView);
        searchData(searchView);
        return super.onCreateOptionsMenu(menu);
    }

    //初始化SearchView的样式
    private void initSearchView(SearchView searchView) {
        //设置提交按钮可见
        searchView.setSubmitButtonEnabled(true);
        //搜索框View，设置样式
        EditText searchEditView = searchView.findViewById(androidx.appcompat.R.id.search_src_text);
        searchEditView.setHint("请输入搜索内容");
        searchEditView.setHintTextColor(getResources().getColor(android.R.color.darker_gray));
        searchEditView.setTextColor(getResources().getColor(R.color.white));
        searchEditView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
        //去掉搜索框默认的下划线
        LinearLayout searchPlate = searchView.findViewById(androidx.appcompat.R.id.search_plate);
        searchPlate.setBackground(null);
    }

    // SearchView的文本变化的监听
    private void searchData(SearchView searchView) {
        //监听文本变化
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                //提交文本时调用
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                //文本搜索框发生变化时调用
                adapter.getFilter().filter(newText);
                return false;
            }
        });
    }


}